首页 > 试题广场 >

三数之和

[编程题]三数之和
  • 热度指数:221611 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给出一个有n个元素的数组S,S中是否有元素a,b,c满足a+b+c=0?找出数组S中所有满足条件的三元组。

数据范围:,数组中各个元素值满足
空间复杂度:,时间复杂度

注意:
  1. 三元组(a、b、c)中的元素必须按非降序排列。(即a≤b≤c)
  2. 解集中不能包含重复的三元组。
例如,给定的数组 S = {-10 0 10 20 -10 -40},解集为(-10, -10, 20),(-10, 0, 10) 
示例1

输入

[0]

输出

[]
示例2

输入

[-2,0,1,1,2]

输出

[[-2,0,2],[-2,1,1]]
示例3

输入

[-10,0,10,20,-10,-40]

输出

[[-10,-10,20],[-10,0,10]]
function threeSum(num) {
  const res = [];
  if (num.length < 3) return res;
  num.sort((a,b)=>a-b);
  for (let i = 0; i < num.length - 2; i++) {
    if (num[i] > 0) return res;
    if (i > 0 && num[i] === num[i - 1]) continue;
    let left = i + 1,
      right = num.length - 1;
    while (left < right) {
      let sum = num[i] + num[left] + num[right];
      if (sum === 0) {
        res.push([num[i], num[left], num[right]]);
        while (left + 1 < right && num[left] === num[left + 1]) {
          left++;
        }
        while (left < right - 1 && num[right] === num[right - 1]) {
          right--;
        }
        left++;
        right--;
      } else if (sum < 0) {
        left++;
      } else if(sum > 0){
        right--;
      }
    }
  }
  return res;
}
排坑,直接调用sort函数排序会出错,需要重写
发表于 2022-10-25 16:36:20 回复(0)
/**
 * 
 * @param num int整型一维数组 
 * @return int整型二维数组
 */
function threeSum( num ) {
    // write code here
    num.sort((a,b)=>a-b)
    var len = num.length
    var res = []
    if (len < 3){
        return res
    }
    for(var i = 0; i < len - 2; i++){
        while (num[i] == num [i - 1]) i++
        var left = i + 1,right = len - 1
        while(left < right){
            var sum = num[i] + num[left] + num[right]
            if(sum == 0){
                res.push([num[i],num[left],num[right]])
                left++
                right--
                while (num[left] == num [left - 1]) left++
                while (num[right] == num [right + 1]) right--
            }else if (sum < 0){
                left++
            }else{
                right--
            }
        }
    }
    return res
}
module.exports = {
    threeSum : threeSum
};

发表于 2022-09-29 10:29:23 回复(0)
/**
 *
 * @param num int整型一维数组
 * @return int整型二维数组
 */


//----------------------------------------终极 讨论大法, 我是闲的蛋疼,才会写出来(居然超过50% 运行和内存)----
function threeSum(num) {
  // write code here

num.sort((a,b)=> a-b)
// console.log(num)
let num_zero=[]
let num_minus=[]
let num_plus=[]
num.forEach(e=>{
    if(e==0){
        num_zero.push(e)
    }
    else if(e<0){
        num_minus.push(e)
    }
    else{
        num_plus.push(e)
    }
})

//等效

let temp_minus = Array.from(new Set(num_minus));
let temp_plus = Array.from(new Set(num_plus));

// let temp_minus=[...new Set(num_minus)]      
// let temp_plus=[...new Set(num_plus)]

let res=[]

if(num_zero.length>=1){

            //1个0参与

            for(let i=0;i<temp_minus.length;i++){
                for(let j=0;j<temp_plus.length;j++){
                    if(temp_minus[i]+temp_plus[j]==0){

                        res.push([temp_minus[i],0,temp_plus[j]])
 
                        break
                    }
                }
            }

            //没有0参与
            //1负两正
            let fir=0

            for(let i=0;i<temp_minus.length;i++){

                fir=0

                for(let j=0;j<num_plus.length;j++){

                    let tem=num_plus.slice(j+1)

                    if(fir==num_plus[j]){
                        continue
                    }

                    for(let n=0;n<tem.length;n++){

                        if(num_plus[j]==tem[n]){
                            fir=tem[n]
                        }
                        if(num_plus[j]+tem[n]+temp_minus[i]==0 ){
                            res.push([temp_minus[i],num_plus[j],tem[n]])
                            break
                        }

                    }

                }
            
            }

            // 1正2负
            let fir_minus=0

            for(let i=0;i<temp_plus.length;i++){

                fir_minus=0   

                for(let j=0;j<num_minus.length;j++){



                    let tem=num_minus.slice(j+1)

                    if(fir_minus==num_minus[j]){
                        continue
                    }
                    for(let n=0;n<tem.length;n++){

                        if(num_minus[j]==tem[n]){
                            fir_minus=tem[n]
                        }
                        if(num_minus[j]+tem[n]+temp_plus[i]==0 ){
                            res.push([num_minus[j],tem[n],temp_plus[i]])
                            break
                        }

                    }

                }
            
            }
            //  有3个以上的0

            if(num_zero.length>=3){
                res.push([0,0,0])
                // console.log(res)
            }
            }

else if(num_zero.length==0){

   //没有0参与
    //1负两正
    let fir=0
    
    for(let i=0;i<temp_minus.length;i++){

        let fir=0
    
        for(let j=0;j<num_plus.length;j++){
    
            let tem=num_plus.slice(j+1)
    
            if(fir==num_plus[j]){
                continue
            }
    
            for(let n=0;n<tem.length;n++){
    
                if(num_plus[j]==tem[n]){
                    fir=tem[n]
                }
                if(num_plus[j]+tem[n]+temp_minus[i]==0 ){
                    res.push([temp_minus[i],num_plus[j],tem[n]])
                    break
                }
    
            }
    
        }
      
    }
    
    // 1正2负
    let fir_minus=0
    
    for(let i=0;i<temp_plus.length;i++){
        fir_minus=0   

    
        for(let j=0;j<num_minus.length;j++){
    
            let tem1=num_minus.slice(j+1)
    
            if(fir_minus==num_minus[j]){
                continue
            }
            for(let n=0;n<tem1.length;n++){
    
                if(num_minus[j]==tem1[n]){
                    fir_minus=tem1[n]
                }
                if(num_minus[j]+tem1[n]+temp_plus[i]==0 ){
                    res.push([num_minus[j],tem1[n],temp_plus[i]])
                    break
                }
    
            }
    
        }
      
    }

}

return res

}
module.exports = {
  threeSum: threeSum,
};

发表于 2022-04-01 20:31:02 回复(0)
/**
 * 
 * @param num int整型一维数组 
 * @return int整型二维数组
 */
function threeSum( num ) {
    // write code here
    const ans=new Set()
    let newNum=num.sort((a,b)=>{
        return a-b
    })
    for(let i=0;i<newNum.length;i++){
        let target=-newNum[i]
        for(let j=i+1;j<newNum.length;j++){
            if(newNum.indexOf(target-newNum[j],j+1)!==-1){
                let index=num.indexOf(target-num[j])
                let temp=[]
                temp.push(num[i])
                temp.push(num[j])
                temp.push(num[index])
                ans.add(temp)
                while(num[j]==num[j+1]) j++  //去重
            }
        }
        while(num[i]==num[i+1]) i++ //去重
    }
    return Array.from(ans)  
}
module.exports = {
    threeSum : threeSum
};
JS题解没人写,我来一个,啊哈哈哈!
发表于 2021-09-26 13:09:56 回复(2)